/*
 * UserNet  --  TCP/IP implementation in userspace
 * Copyright (C) 2010  Robert Millan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdint.h>

#include "unet/unet.h"
#include "unet/misc.h"
#include "unet/rpc.h"
#include "unet/ip.h"
#include "unet/udp.h"
#include "unet/tcp.h"

static const struct in_addr ip_addr_any = { .s_addr = 0 };

struct unet_transport
{
  void *(*listen) (struct in_addr listen_addr, uint16_t port, int n);
  void (*listen_close) (void *this);
  void *(*connect) (struct in_addr dest_addr, uint16_t src_port, uint16_t dst_port);
  void *(*accept) (void *this);
  void (*conn_close) (void *this);
  void (*send) (void *this, const void *buf, size_t n);
  size_t (*recv) (void *this, void *buf, size_t n);
};

static inline void *
udp_connect (struct in_addr dest_addr, uint16_t src_port, uint16_t dst_port)
{
  return unet_udp_connect (dest_addr, src_port, dst_port);
}

static inline void
udp_send (void *this, const void *buf, size_t n)
{
  unet_udp_send (this, buf, n);
}

static inline size_t
udp_recv (void *this, void *buf, size_t n)
{
  return unet_udp_recv (this, buf, n);
}

static inline void
udp_conn_close (void *this)
{
  unet_udp_conn_close (this);
}

static inline void *
tcp_listen (struct in_addr listen_addr, uint16_t port, int n)
{
  return unet_tcp_listen (listen_addr, port, n);
}

static inline void
tcp_listen_close (void *this)
{
  unet_tcp_listen_close (this);
}

static inline void *
tcp_connect (struct in_addr dest_addr, uint16_t src_port, uint16_t dst_port)
{
  return unet_tcp_connect (dest_addr, src_port, dst_port);
}

static inline void *
tcp_accept (void *this)
{
  return unet_tcp_accept (this);
}

static inline void
tcp_send (void *this, const void *buf, size_t n)
{
  unet_tcp_send (this, buf, n);
}

static inline size_t
tcp_recv (void *this, void *buf, size_t n)
{
  return unet_tcp_recv (this, buf, n);
}

static inline void
tcp_conn_close (void *this)
{
  unet_tcp_conn_close (this);
}

static struct unet_transport unet_transport[] =
  {
    {
      .listen = tcp_listen,
      .listen_close = tcp_listen_close,
      .connect = tcp_connect,
      .accept = tcp_accept,
      .conn_close = tcp_conn_close,
      .send = tcp_send,
      .recv = tcp_recv,
    },
    {
      .connect = udp_connect,
      .conn_close = udp_conn_close,
      .send = udp_send,
      .recv = udp_recv,
    },
  };

int
unet_init ()
{
  if (unet_rpc_init () == -1)
    return -1;

  return 0;
}

void
unet_fini ()
{
  unet_rpc_fini ();
}

unet_listen_t
unet_listen (enum ip_protocols protocol, struct in_addr listen_addr, uint16_t port, int n)
{
  unet_listen_t listen;

  if (unet_rpc_port (protocol, UNET_RPC_PORT_OPEN, listen_addr, &port) == -1)
    return NULL;

  listen = unet_zalloc (sizeof (*listen));
  if (! listen)
    return NULL;

  listen->protocol = protocol;
  listen->addr = listen_addr;
  listen->port = port;
  listen->state = unet_transport[protocol].listen (listen_addr, port, n);

  return listen;
}

void
unet_listen_close (unet_listen_t this)
{
  unet_rpc_port (this->protocol, UNET_RPC_PORT_CLOSE, this->addr, &this->port);

  unet_transport[this->protocol].listen_close (this->state);

  free (this);
}

unet_connect_t
unet_connect (enum ip_protocols protocol, struct in_addr dest_addr, uint16_t dst_port)
{
  unet_connect_t connection;

  connection = unet_zalloc (sizeof (*connection));
  if (! connection)
    return NULL;

  assert (connection->src_port == 0);

  if (unet_rpc_port (protocol, UNET_RPC_PORT_OPEN, ip_addr_any, &connection->src_port) == -1)
    goto fail;

  connection->protocol = protocol;
  connection->state = unet_transport[connection->protocol].connect (dest_addr, connection->src_port, dst_port);
  return connection;

 fail:
  free (connection);
  return NULL;
}

unet_connect_t
unet_accept (unet_listen_t this)
{
  unet_connect_t connection = unet_zalloc (sizeof (*connection));
  if (! connection)
    return NULL;

  connection->src_port = 0;
  connection->protocol = this->protocol;
  connection->state = unet_transport[connection->protocol].accept (this->state);
  return connection;
}

void
unet_send (unet_connect_t connection, const void *buf, size_t n)
{
  unet_transport[connection->protocol].send (connection->state, buf, n);
}

size_t
unet_recv (unet_connect_t connection, void *buf, size_t n)
{
  return unet_transport[connection->protocol].recv (connection->state, buf, n);
}

void
unet_conn_close (unet_connect_t connection)
{
  if (connection->src_port != 0)
    (void) unet_rpc_port (connection->protocol, UNET_RPC_PORT_CLOSE, ip_addr_any, &connection->src_port);

  unet_transport[connection->protocol].conn_close (connection->state);
  free (connection);
}
